#!/bin/bash

LogFile=/tmp/HmcInstall.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
function ExitCleanup {
    # keep the log file, but ensure a different user can overwrite it next time
    cd /
    chmod 666 $LogFile

    if [ $1 -ne 0 ]; then
        exit $1
    else
        exit 0
    fi
}

#-------------------------------------------------------------------------------
# This function does the RPM installation task.
#-------------------------------------------------------------------------------
function InstallRpm {
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        # Strip version then leading directory name
        f=`echo ${x%%-[0-9]*}`
        r=`echo ${f##*/}`
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$r" == "$i" ]; then
                return
            fi
        done
    fi

    CMD=`echo $1`; shift;
    RPM=`echo $1`; shift;
    OPT=`echo $*`
    if [ "$OPT" == "" ]; then
        OPT="--force --nodeps"
    fi

    # Log the test install output. The format for the "normal" RPM install
    # processing inside this script is 'rpm -i <file spec> --force --nodeps'
    if [ -f $RPM ]; then
        echo "=====================================================" >> $LogFile
        echo "***** Executing rpm -vv $CMD $RPM $OPT *****         " >> $LogFile
        echo "=====================================================" >> $LogFile
        rpm -vv $CMD $RPM $OPT >> $LogFile 2>&1
        if [ $? -ne 0 ]; then
            echo "Error installing rpm fileset named $2" >> $LogFile
            if [ "$Update" == "true" ]; then
                echo "Error installing rpm fileset named $2"
                ExitCleanup 9
            fi
        fi
    fi
}

#-------------------------------------------------------------------------------
# This function does the RPM removal task.
#-------------------------------------------------------------------------------
function EraseRpm {
    # If rpm is part of no update list then do not do anything with it
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$x" == "$i" ]; then
                return
            fi
        done
    fi
    echo "=====================================================" >> $LogFile
    echo "***** Executing rpm -evv $* *****                    " >> $LogFile
    echo "=====================================================" >> $LogFile
    rpm -evv $* --nodeps --allmatches >> $LogFile 2>&1
    if [ $? -ne 0 ]; then
        echo "Error removing rpm fileset named $2" >> $LogFile
    fi
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" == "" ]; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    ExitCleanup 1
fi

# Check if directory exists
if [ ! -d $image ]; then
    echo "The directory $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    ExitCleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]; then
    rm -f /tmp/saved_files.tar
    cat /opt/hsc/data/config/NO_UPDATE_FILES | \
        xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ ! -d /opt/IBMJava ]; then
    ln -sf /opt/IBMJava2-142 /opt/IBMJava
fi

# Remove the zip file to save space
rm -f /usr/local/hsc_install.images/*.zip

VER=`/opt/hsc/bin/hsc version | grep Version | cut -d':' -f2 |awk '{print $1}'`
REL=`/opt/hsc/bin/hsc version | grep Release | cut -d':' -f2`
REL=`echo $REL | cut -d'.' -f1|awk '{print $1}'`

if [[ "$VER" == "4"  &&  "$REL" == "4" ]]; then
    rm -f /tmp/WsmSaved.tar
    /bin/tar cf /tmp/WsmSaved.tar /var/websm/security /usr/websm/codebase/SM* \
       /var/websm/config/user_settings/websm.cfg 2>/dev/null

    rm /opt/hsc/com/ibm/hsc/websm/launch/hscmgt/rpminst.dat 2>/dev/null
    # Now handle info center
    if [ ! -f /opt/hsc/data/extbackupfile.list ]; then
        touch /opt/hsc/data/extbackupfile.list
    fi
    il=`find /opt/bobcat/ -type f -print`
    for i in $il
    do
       grep -q "$i" /opt/hsc/data/extbackupfile.list
       if [ $? -ne 0 ]; then
            echo "$i" >> /opt/hsc/data/extbackupfile.list
       fi
    done
    il=`find /opt/bobcat/ -type l -print`
    for i in $il
    do
       grep -q "$i" /opt/hsc/data/extbackupfile.list
       if [ $? -ne 0 ]; then
            echo "$i" >> /opt/hsc/data/extbackupfile.list
       fi
    done

    mv /opt/hsc/data/version /tmp

    # Save old log files
    if [ -f $LogFile ]; then
        mv -f ${LogFile}.3 ${LogFile}.4 2>/dev/null
        mv -f ${LogFile}.2 ${LogFile}.3 2>/dev/null
        mv -f ${LogFile}.1 ${LogFile}.2 2>/dev/null
        mv -f ${LogFile}   ${LogFile}.1 2>/dev/null
    fi

    echo "=========================================================" >> $LogFile
    echo "*********   Performing Update operation `date`  *********" >> $LogFile
    echo "=========================================================" >> $LogFile

    if [ -d $image/websm ]; then
        cd $image/websm
        echo "--- Installing WebSM...."
        EraseRpm sysmgt.websm.security
        InstallRpm -ivh sysmgt.websm.security-[0123456789]*.rpm
        if [ ! -f /usr/websm/codebase/pluginjars/bundles.jar ]; then
            ln -sf /usr/websm/codebase/bundles.jar /usr/websm/codebase/pluginjars/
        fi
    fi

    # Install the base HMC rpms if baseHMC directory exists
    if [ -d $image/baseHMC ]; then
        cd $image/baseHMC
        echo "--- Installing Common Console Framework...."
        /usr/sbin/groupadd -g 508 ccfw 2>/dev/null
        EraseRpm ccfw
        if [ "$Update" == "false" ]; then
            rm -rf /opt/ccfw
        fi
        InstallRpm -ivh ccfw*.rpm
        /usr/sbin/useradd -c "Common Console Framework" -M -g ccfw -d /opt/ccfw ccfw
        InstallRpm -Uvh IBMhsc.coreserver*.rpm

        grep -q  "MH00222: Required V4 R4.0 maintenance (03-14-2005)" /tmp/version
        if [ $? -ne 0 ]; then
            echo "MH00222: Required V4 R4.0 maintenance (03-14-2005)" >> /tmp/version
        fi
        cp -f /tmp/version /opt/hsc/data/version
        cat /opt/hsc/data/version > /root/.version
        cat /opt/hsc/data/version > /home/hscroot/.version
        chmod 644 /home/hscroot/.version
        chown hscroot.hmc /home/hscroot/.version
    fi

    cd /
    /bin/tar -xf /tmp/WsmSaved.tar
    /bin/tar -xf $image/hsc.tar

    if [ -f $image/postinstall ]; then
        $image/postinstall
    fi

    echo "=========================================================" >> $LogFile
    echo "********* Install/Update complete at `date` *********"     >> $LogFile
    echo "=========================================================" >> $LogFile
else
    echo "---- Cannot install this efix(MH00222) on this HMC Level ---"
fi
ExitCleanup 0
